<template>
  <div>
    <div class="container-fluid">
      <div class="common_title">用户信息修改</div>
      <div class="container-fluid common_con">
        <form class="form-horizontal article_form" id="form" ref="form">
          <div class="form-group">
            <label for="inputEmail1" class="col-sm-2 control-label"
              >用户名称：</label
            >
            <div class="col-sm-4">
              <input
                type="text"
                name="username"
                class="form-control username"
                id="inputEmail1"
                v-model="userInfo.username"
              />
            </div>
          </div>
          <div class="form-group">
            <label for="inputEmail2" class="col-sm-2 control-label"
              >昵称：</label
            >
            <div class="col-sm-4">
              <input
                type="text"
                name="nickname"
                class="form-control nickname"
                id="inputEmail2"
                v-model="userInfo.nickname"
              />
            </div>
          </div>
          <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label"
              >邮箱：</label
            >
            <div class="col-sm-4">
              <input
                type="text"
                name="email"
                class="form-control email"
                id="inputEmail3"
                v-model="userInfo.email"
              />
            </div>
          </div>

          <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label"
              >用户图标：</label
            >
            <div class="col-sm-10">
              <img :src="userInfo.userPic" alt="" class="user_pic" />
              <input
                style="display: none"
                name="userPic"
                type="file"
                id="exampleInputFile"
                @change="doChange"
                ref="file"
              />
              <!-- 给label标签的for属性赋值,值是input标签的id,那这样的话点击label就相当于点击了input -->
              <br />
              <label for="exampleInputFile">
                <img
                  src="~@/assets/images/uploads_icon.jpg"
                  style="height: 50px"
                  alt=""
                />
              </label>
            </div>
          </div>

          <div class="form-group">
            <label for="inputEmail4" class="col-sm-2 control-label"
              >密码：</label
            >
            <div class="col-sm-4">
              <input
                type="password"
                name="password"
                class="form-control password"
                id="inputEmail4"
                v-model="userInfo.password"
              />
            </div>
          </div>
          <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
              <button
                type="submit"
                class="btn btn-success btn-edit"
                @click.prevent="doEdit"
              >
                修改
              </button>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script>
// 导入接口
import { editUserInfo } from "./user.js";
export default {
  name: "user",
  data() {
    return {
      userInfo: {},
    };
  },
  methods: {
    // 文件预览
    doChange() {
      // (1)获取文件数据
      let file = this.$refs.file.files[0];
      console.log(file);
      // (2)生成blob路径显示到页面
      this.userInfo.userPic = URL.createObjectURL(file);
    },

    // 编辑个人信息
    async doEdit() {
      // (1)创建formdata对象
      let fd = new FormData(this.$refs.form);
      // (2)调用接口
      let res = await editUserInfo(fd);
      console.log(res);
      // (3)弹窗提示
      this.$alert("修改成功");
      // (4)更新vuex
      this.$store.commit('updateUserInfo', this.userInfo);
    },
  },
  created() {
    //  疑惑:为什么这里不能使用mapState辅助函数
    // (1)因为这个页面需要使用表单双向绑定,如果是mapState辅助函数,当我们修改表单数据,vuex的数据也会修改
    // vuex数据不能直接修改
    // (2)什么时候修改vuex数据?  :用户点击编辑,服务器返回修改成功

    this.userInfo = {
      ...this.$store.state.userInfo

      // this.userInfo.username=this.$store.state.userInfo.username
      // this.userInfo.nickname=this.$store.state.userInfo.nickname
      // this.userInfo.password=this.$store.state.userInfo.password
      // this.userInfo.email=this.$store.state.userInfo.email
      // this.userInfo.userPic=this.$store.state.userInfo.userPic
    }
  },
};
</script>

<style>
</style>